import 'package:finance_app/src/entities/expense_detail_entity.dart';
import 'package:finance_app/src/entities/account_entity.dart';
import 'package:finance_app/src/entities/business_contact_entity.dart';
import 'package:finance_app/src/entities/format/split_expense_entity.dart';
import 'package:finance_app/src/entities/ocr_attachment_entity.dart';
import 'package:finance_app/src/entities/payment_entity.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/utils/cash_utils.dart';

expenseDetailEntityFromJson(ExpenseDetailEntity data, Map<String, dynamic> json) {
	if (json['data_type'] != null) {
		data.dataType = json['data_type'].toString();
	}
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['company_id'] != null) {
		data.companyId = json['company_id'] is String
				? int.tryParse(json['company_id'])
				: json['company_id'].toInt();
	}
	if (json['contact_id'] != null) {
		data.contactId = json['contact_id'] is String
				? int.tryParse(json['contact_id'])
				: json['contact_id'].toInt();
	}
	if (json['account_id'] != null) {
		data.accountId = json['account_id'] is String
				? int.tryParse(json['account_id'])
				: json['account_id'].toInt();
	}
	if (json['contact'] != null) {
		data.contact = BusinessContactEntity().fromJson(json['contact']);
	}
	if (json['number'] != null) {
		data.number = json['number'].toString();
	}
	if (json['issued_at'] != null) {
		data.issuedAt = json['issued_at'].toString();
	}
	if (json['items'] != null) {
		data.items = (json['items'] as List).map((v) => ExpenseDetailItems().fromJson(v)).toList();
	}
	if (json['subtotal'] != null) {
		data.subtotal = json['subtotal'] is String
				? num.tryParse(json['subtotal'])
				: json['subtotal'];
	}
	if (json['inclusive_tax_amount'] != null) {
		data.inclusiveTaxAmount = json['inclusive_tax_amount'] is String
				? num.tryParse(json['inclusive_tax_amount'])
				: json['inclusive_tax_amount'];
	}
	if (json['exclusive_tax_amount'] != null) {
		data.exclusiveTaxAmount = json['exclusive_tax_amount'] is String
				? num.tryParse(json['exclusive_tax_amount'])
				: json['exclusive_tax_amount'];
	}
	if (json['total'] != null) {
		data.total = json['total'] is String
				? num.tryParse(json['total'])
				: json['total'];
	}
	if (json['unpaid'] != null) {
		data.unpaid = json['unpaid'] is String
				? num.tryParse(json['unpaid'])
				: json['unpaid'];
	}
	if (json['paid_using'] != null) {
		data.paidUsing = json['paid_using'].toString();
	}
	if (json['paid_by_employee_id'] != null) {
		data.paidByEmployeeId = json['paid_by_employee_id'];
	}
	if (json['payment_type_id'] != null) {
		data.paymentTypeId = json['payment_type_id'] is String
				? int.tryParse(json['payment_type_id'])
				: json['payment_type_id'].toInt();
	}
	if (json['reimburse_status'] != null) {
		data.reimburseStatus = json['reimburse_status'];
	}
	if (json['reimburse_at'] != null) {
		data.reimburseAt = json['reimburse_at'].toString();
	}
	if (json['rejected_reason'] != null) {
		data.rejectedReason = json['rejected_reason'];
	}
	if (json['notes'] != null) {
		data.notes = json['notes'].toString();
	}
	if (json['status'] != null) {
		data.status = json['status'].toString();
	}
	if (json['created_by'] != null) {
		data.createdBy = json['created_by'];
	}
	if (json['created_at'] != null) {
		data.createdAt = json['created_at'].toString();
	}
	if (json['updated_at'] != null) {
		data.updatedAt = json['updated_at'].toString();
	}
	if (json['account'] != null) {
		data.account = AccountEntity().fromJson(json['account']);
	}
	if (json['currency_code'] != null) {
		data.currencyCode = json['currency_code'].toString();
	}
	if (json['attachments'] != null) {
		data.attachments = (json['attachments'] as List).map((v) => OcrAttachmentEntity().fromJson(v)).toList();
	}
	if (json['payment_type'] != null) {
		data.paymentType = SettingDataPaymentType().fromJson(json['payment_type']);
	}
	if (json['reimburse_payment_type'] != null) {
		data.reimbursePaymentType = SettingDataPaymentType().fromJson(json['reimburse_payment_type']);
	}
	if (json['paid_by'] != null) {
		data.paidBy = json['paid_by'].toString();
	}
	if (json['due_at'] != null) {
		data.dueAt = json['due_at'].toString();
	}
	if (json['is_overdue'] != null) {
		data.isOverdue = json['is_overdue'] is String
				? int.tryParse(json['is_overdue'])
				: json['is_overdue'].toInt();
	}
	if (json['payslip_is_pay'] != null) {
		data.isPayslipPaid = json['payslip_is_pay'] is String
				? int.tryParse(json['payslip_is_pay'])
				: json['payslip_is_pay'].toInt();
	}
	if (json['employee_id'] != null) {
		data.employeeId = json['employee_id'] is String
				? int.tryParse(json['employee_id'])
				: json['employee_id'].toInt();
	}
	if (json['business_contact_name'] != null) {
		data.businessContactName = json['business_contact_name'].toString();
	}
	if (json['payments'] != null) {
		data.payments = (json['payments'] as List).map((v) => PaymentEntity().fromJson(v)).toList();
	}
	return data;
}

Map<String, dynamic> expenseDetailEntityToJson(ExpenseDetailEntity entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['data_type'] = entity.dataType;
	data['id'] = entity.id;
	data['company_id'] = entity.companyId;
	data['contact_id'] = entity.contactId;
	data['account_id'] = entity.accountId;
	data['contact'] = entity.contact?.toJson();
	data['number'] = entity.number;
	data['issued_at'] = entity.issuedAt;
	data['items'] =  entity.items?.map((v) => v.toJson())?.toList();
	data['subtotal'] = entity.subtotal;
	data['inclusive_tax_amount'] = entity.inclusiveTaxAmount;
	data['exclusive_tax_amount'] = entity.exclusiveTaxAmount;
	data['total'] = entity.total;
	data['unpaid'] = entity.unpaid;
	data['paid_using'] = entity.paidUsing;
	data['paid_by_employee_id'] = entity.paidByEmployeeId;
	data['payment_type_id'] = entity.paymentTypeId;
	data['reimburse_status'] = entity.reimburseStatus;
	data['reimburse_at'] = entity.reimburseAt;
	data['rejected_reason'] = entity.rejectedReason;
	data['notes'] = entity.notes;
	data['status'] = entity.status;
	data['created_by'] = entity.createdBy;
	data['created_at'] = entity.createdAt;
	data['updated_at'] = entity.updatedAt;
	data['account'] = entity.account?.toJson();
	data['currency_code'] = entity.currencyCode;
	data['attachments'] =  entity.attachments?.map((v) => v.toJson())?.toList();
	data['payment_type'] = entity.paymentType?.toJson();
	data['reimburse_payment_type'] = entity.reimbursePaymentType?.toJson();
	data['paid_by'] = entity.paidBy;
	data['due_at'] = entity.dueAt;
	data['is_overdue'] = entity.isOverdue;
	data['payslip_is_pay'] = entity.isPayslipPaid;
	data['employee_id'] = entity.employeeId;
	data['business_contact_name'] = entity.businessContactName;
	data['payments'] =  entity.payments?.map((v) => v.toJson())?.toList();
	return data;
}

expenseDetailItemsFromJson(ExpenseDetailItems data, Map<String, dynamic> json) {
	if (json['data_type'] != null) {
		data.dataType = json['data_type'].toString();
	}
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['company_id'] != null) {
		data.companyId = json['company_id'] is String
				? int.tryParse(json['company_id'])
				: json['company_id'].toInt();
	}
	if (json['expense_id'] != null) {
		data.expenseId = json['expense_id'] is String
				? int.tryParse(json['expense_id'])
				: json['expense_id'].toInt();
	}
	if (json['category_id'] != null) {
		data.categoryId = json['category_id'] is String
				? int.tryParse(json['category_id'])
				: json['category_id'].toInt();
	}
	if (json['category_key'] != null) {
		data.categoryKey = json['category_key'].toString();
	}
	if (json['price'] != null) {
		data.price = json['price'] is String
				? num.tryParse(json['price'])
				: json['price'];
	}
	if (json['nett_price'] != null) {
		data.nettPrice = json['nett_price'] is String
				? num.tryParse(json['nett_price'])
				: json['nett_price'];
	}
	if (json['is_tax_inclusive'] != null) {
		data.isTaxInclusive = json['is_tax_inclusive'] is String
				? int.tryParse(json['is_tax_inclusive'])
				: json['is_tax_inclusive'].toInt();
	}
	if (json['tax_amount'] != null) {
		data.taxAmount = json['tax_amount'] is String
				? num.tryParse(json['tax_amount'])
				: json['tax_amount'];
	}
	if (json['created_at'] != null) {
		data.createdAt = json['created_at'].toString();
	}
	if (json['updated_at'] != null) {
		data.updatedAt = json['updated_at'].toString();
	}
	if (json['tax'] != null) {
		data.tax = SettingDataTax().fromJson(json['tax']);
	}
	if (json['category'] != null) {
		data.category = SettingDataCategory().fromJson(json['category']);
	}
	return data;
}

Map<String, dynamic> expenseDetailItemsToJson(ExpenseDetailItems entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['data_type'] = entity.dataType;
	data['id'] = entity.id;
	data['company_id'] = entity.companyId;
	data['expense_id'] = entity.expenseId;
	data['category_id'] = entity.categoryId;
	data['category_key'] = entity.categoryKey;
	data['price'] = entity.price;
	data['nett_price'] = entity.nettPrice;
	data['is_tax_inclusive'] = entity.isTaxInclusive;
	data['tax_amount'] = entity.taxAmount;
	data['created_at'] = entity.createdAt;
	data['updated_at'] = entity.updatedAt;
	data['tax'] = entity.tax?.toJson();
	data['category'] = entity.category?.toJson();
	return data;
}